package Reminder.view.pages;
import Reminder.dao.RemindDao;
import Reminder.dao.ScheduleDao;
import Reminder.dao.impl.RemindDaoImpl;
import Reminder.dao.impl.ScheduleDaoImpl;
import Reminder.domain.Course;
import Reminder.domain.Message;
import Reminder.domain.Period;
import Reminder.domain.Schedule;
import Reminder.view.Controller;
import Reminder.view.GlobalConfig;
import Reminder.view.Main;
import Reminder.view.components.ScheduleCell;
import com.alibaba.fastjson.JSONObject;
import com.jfoenix.controls.*;
import com.jfoenix.svg.SVGGlyph;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.ButtonBar;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
import javafx.scene.control.DialogPane;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import java.awt.*;
import java.io.IOException;
import java.text.ParseException;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Optional;

public class pageSchedule extends Page{
    BorderPane root = new BorderPane();
    String selectCourse;
    JFXTextField courseField;
    JFXTextField deletePeriodField;
    JFXTextField deleteCourseField;
    int deletePeriodIndex = -1;
    int deleteCourseIndex = -1;
    public pageSchedule(Stage stage, Controller controller) {
        super(stage, controller);

        JFXScrollPane pane = new JFXScrollPane();

        BorderPane buttonBar = new BorderPane();
        JFXButton backButton = new JFXButton("");
        SVGGlyph arrow = new SVGGlyph(0,
                "FULLSCREEN",
                "M402.746 877.254l-320-320c-24.994-24.992-24.994-65.516 0-90.51l320-320c24.994-24.992 65.516-24.992 90.51 0 24.994 24.994 "
                        + "24.994 65.516 0 90.51l-210.746 210.746h613.49c35.346 0 64 28.654 64 64s-28.654 64-64 64h-613.49l210.746 210.746c12.496 "
                        + "12.496 18.744 28.876 18.744 45.254s-6.248 32.758-18.744 45.254c-24.994 24.994-65.516 24.994-90.51 0z",
                Color.WHITE);

        arrow.setOnMouseClicked(
                new EventHandler<MouseEvent>() {
                    @Override
                    public void handle(MouseEvent mouseEvent) {
                        GlobalConfig.WINDOW_CURRENT_WIDTH = getStage().getScene().getWidth();
                        GlobalConfig.WINDOW_CURRENT_HEIGHT = getStage().getScene().getHeight();
//                            getStage().hide();
                        try {
                            pageOptions pageOptions = new pageOptions(getStage(),getController());
                        } catch (IOException | ParseException | AWTException e) {
                            e.printStackTrace();
                        }
//                            getStage().show();
                    }
                }
        );
        arrow.setSize(20, 16);
        backButton.setGraphic(arrow);
        backButton.setRipplerFill(Color.WHITE);
        buttonBar.setLeft(backButton);
        buttonBar.setPadding(new javafx.geometry.Insets(20));
        buttonBar.setStyle("-fx-background-color: green");



        HBox rightToolBar = new HBox();
        JFXButton deleteCourse = new JFXButton("");
        SVGGlyph deleteCourseIcon = new SVGGlyph(0, "FULLSCREEN", "M516.693333 103.338667a154.453333 154.453333 0 0 0-154.325333 149.461333H167.509333a39.381333 39.381333 0 0 0 0 78.72h43.434667v440.32c0 82.688 56.021333 150.272 125.312 150.272h352.938667c69.12 0 125.312-67.2 125.312-150.229333V331.776h39.381333a39.381333 39.381333 0 0 0 0-78.72H670.72a154.026667 154.026667 0 0 0-154.026667-149.717333zM436.053333 252.8a80.64 80.64 0 0 1 80.682667-75.733333c43.050667 0 77.824 33.194667 80.298667 75.733333h-160.981334zM336.128 848.512c-24.533333 0-51.584-31.530667-51.584-76.629333V331.776h456.106667V772.266667c0 45.013333-27.050667 76.629333-51.584 76.629333H336.128v-0.426667z m52.864-94.634667c17.962667 0 32.810667-17.962667 32.810667-40.533333v-231.338667c0-22.570667-14.72-40.533333-32.853334-40.533333-17.92 0-32.768 17.962667-32.768 40.533333v231.338667c0 22.442667 14.421333 40.533333 32.810667 40.533333z m118.741333 0c17.962667 0 32.810667-17.962667 32.810667-40.533333v-231.338667c0-22.570667-14.72-40.533333-32.810667-40.533333-17.962667 0-32.810667 17.962667-32.810666 40.533333v231.338667c0 22.442667 14.848 40.533333 32.853333 40.533333z m124.928 0c17.962667 0 32.810667-17.962667 32.810667-40.533333v-231.338667c0-22.570667-14.72-40.533333-32.853333-40.533333-17.92 0-32.768 17.962667-32.768 40.533333v231.338667c0 22.442667 14.293333 40.533333 32.810666 40.533333z",
                Color.WHITE);
        deleteCourseIcon.setSize(20);

        deleteCourse.setGraphic(deleteCourseIcon);

        deleteCourse.setText("删除课程");
        deleteCourse.setStyle("-fx-text-fill: white");
        deleteCourse.setRipplerFill(Color.WHITE);

        deleteCourse.setOnMouseClicked(
                new EventHandler<MouseEvent>() {
                    @Override
                    public void handle(MouseEvent mouseEvent) {
                        try {
                            onDeleteCourse();
                        } catch (AWTException | IOException | ParseException e) {
                            e.printStackTrace();
                        }
                    }
                }
        );
        JFXButton deletePeriod = new JFXButton("");
        SVGGlyph deletePeriodIcon = new SVGGlyph(0, "FULLSCREEN", "M512 0C230.4 0 0 230.4 0 512s230.4 512 512 512 512-230.4 512-512S793.6 0 512 0z m0 938.666667C277.333333 938.666667 85.333333 746.666667 85.333333 512S277.333333 85.333333 512 85.333333s426.666667 192 426.666667 426.666667-192 426.666667-426.666667 426.666667z M341.333333 469.333333h341.333334v85.333334H341.333333z",
                Color.WHITE);
        deletePeriodIcon.setSize(20);

        deletePeriod.setGraphic(deletePeriodIcon);

        deletePeriod.setText("删除课时");
        deletePeriod.setStyle("-fx-text-fill: white");
        deletePeriod.setRipplerFill(Color.WHITE);

        deletePeriod.setOnMouseClicked(
                new EventHandler<MouseEvent>() {
                    @Override
                    public void handle(MouseEvent mouseEvent) {
                        try {
                            onDeletePeriod();
                        } catch (AWTException | IOException | ParseException e) {
                            e.printStackTrace();
                        }
                    }
                }
        );

        JFXButton addPeriod = new JFXButton("");
        SVGGlyph periodIcon = new SVGGlyph(0, "FULLSCREEN", "M874.0352 149.956267C777.335467 53.256533 648.7552 0 512 0 375.236267 0 246.664533 53.248 149.956267 149.956267 53.256533 246.664533 0 375.236267 0 512s53.248 265.335467 149.956267 362.043733C246.664533 970.743467 375.236267 1024 512 1024s265.335467-53.248 362.043733-149.956267C970.743467 777.335467 1024 648.763733 1024 512s-53.256533-265.335467-149.9648-362.043733zM554.666667 469.333333V315.682133A42.606933 42.606933 0 0 0 512 273.066667a42.666667 42.666667 0 0 0-42.666667 42.615466V469.333333H315.682133A42.606933 42.606933 0 0 0 273.066667 512a42.666667 42.666667 0 0 0 42.615466 42.666667H469.333333v153.6512A42.606933 42.606933 0 0 0 512 750.933333a42.666667 42.666667 0 0 0 42.666667-42.615466V554.666667h153.6512A42.606933 42.606933 0 0 0 750.933333 512a42.666667 42.666667 0 0 0-42.615466-42.666667H554.666667z m259.029333 344.362667C733.115733 894.293333 625.962667 938.666667 512 938.666667c-113.962667 0-221.115733-44.381867-301.696-124.970667C129.706667 733.1072 85.333333 625.962667 85.333333 512c0-113.962667 44.381867-221.115733 124.970667-301.696C290.8928 129.706667 398.037333 85.333333 512 85.333333c113.962667 0 221.115733 44.381867 301.696 124.970667C894.293333 290.884267 938.666667 398.037333 938.666667 512c0 113.9712-44.381867 221.115733-124.970667 301.696z",
                                Color.WHITE);
        periodIcon.setSize(20);

        addPeriod.setGraphic(periodIcon);
        
        addPeriod.setText("添加课时");
        addPeriod.setStyle("-fx-text-fill: white");
        addPeriod.setRipplerFill(Color.WHITE);

        JFXButton addCourse = new JFXButton("");
        SVGGlyph courseIcon = new SVGGlyph(0, "FULLSCREEN", "M919.84 314.912L673.024 72.928c-6.08-5.952-14.016-8.928-22.752-9.152-8.512 0.096-16.64 3.552-22.56 9.632l-459.2 470.4c-4.416 4.512-7.392 10.272-8.576 16.448L97.984 890.912c-1.984 10.592 1.472 21.44 9.216 28.896 6.016 5.856 14.016 8.992 22.24 8.992 2.304 0 4.672-0.256 6.976-0.768l292.448-65.216c6.112-1.344 11.712-4.512 16.064-9.024l475.584-493.824c12.16-12.672 11.872-32.768-0.672-45.056zM433.792 773.088L311.264 651.616c-12.576-12.48-32.864-12.384-45.248 0.192-12.448 12.576-12.352 32.8 0.192 45.248l112.384 111.392-208.768 46.592 51.264-273.472 430.048-440.48L852.384 338.4 433.792 773.088z",
                                Color.WHITE);
        addPeriod.setOnMouseClicked(
                new EventHandler<MouseEvent>() {
                    @Override
                    public void handle(MouseEvent mouseEvent) {
                        try {
                            onAddPeriod();
                        } catch (AWTException | IOException | ParseException e) {
                            e.printStackTrace();
                        }
                    }
                }
        );
        courseIcon.setSize(20);

        addCourse.setGraphic(courseIcon);
        addCourse.setText("添加课程");
        addCourse.setStyle("-fx-text-fill: white");
        addCourse.setRipplerFill(Color.WHITE);
        addCourse.setOnMouseClicked(
                new EventHandler<MouseEvent>() {
                    @Override
                    public void handle(MouseEvent mouseEvent) {
                        try {
                            onAddCourse();
                        } catch (AWTException | IOException | ParseException e) {
                            e.printStackTrace();
                        }
                    }
                }
        );

        JFXButton setButton = new JFXButton("");
        SVGGlyph set = new SVGGlyph(0, "FULLSCREEN", "M307.2 269.0048a306.8928 306.8928 0 0 1 108.8-62.9248 25.6 25.6 0 0 1 28.0064 8.3456c16.4352 20.48 41.1648 32.6656 67.9936 32.6656s51.5584-12.1344 67.9936-32.6656a25.6 25.6 0 0 1 28.0064-8.3456c40.2432 13.2608 77.312 34.7136 108.8 62.9248a25.6 25.6 0 0 1 6.7584 28.3648 86.8352 86.8352 0 0 0 5.7344 75.1616c13.3632 23.2448 36.3008 38.5536 62.2592 42.5472a25.6 25.6 0 0 1 21.1968 20.1216 308.4288 308.4288 0 0 1 0 125.5936 25.6 25.6 0 0 1-21.1968 20.0704 86.8352 86.8352 0 0 0-62.2592 42.5472c-13.4144 23.1936-15.2576 50.688-5.7344 75.2128a25.6 25.6 0 0 1-6.7584 28.3648c-31.5392 28.16-68.5568 49.664-108.8 62.8736a25.6 25.6 0 0 1-28.0064-8.2944A86.8352 86.8352 0 0 0 512 748.8512c-26.8288 0-51.5584 12.1856-67.9936 32.7168a25.6 25.6 0 0 1-28.0064 8.2944A306.8928 306.8928 0 0 1 307.2 727.04a25.6 25.6 0 0 1-6.7584-28.3648 86.8352 86.8352 0 0 0-5.7344-75.2128 86.8352 86.8352 0 0 0-62.2592-42.496 25.6 25.6 0 0 1-21.1968-20.1216 308.4288 308.4288 0 0 1 0-125.5936 25.6 25.6 0 0 1 21.1968-20.1216 86.8352 86.8352 0 0 0 62.2592-42.496c13.4144-23.2448 15.2576-50.688 5.7344-75.264A25.6 25.6 0 0 1 307.2 269.056z m31.8976 129.1264a137.8816 137.8816 0 0 1-80.5376 63.488 258.4064 258.4064 0 0 0 0 72.7552c33.536 9.8816 62.464 32.256 80.5376 63.488 18.0224 31.1808 22.8352 67.4304 14.6944 101.376 19.1488 15.104 40.3456 27.392 62.976 36.4544a137.8816 137.8816 0 0 1 95.232-38.0416c36.096 0 69.888 13.9264 95.232 38.0416a255.6928 255.6928 0 0 0 62.976-36.4544 137.8816 137.8816 0 0 1 14.6944-101.376 137.8816 137.8816 0 0 1 80.5376-63.488 258.4064 258.4064 0 0 0 0-72.8064 137.8816 137.8816 0 0 1-80.5376-63.4368 137.8816 137.8816 0 0 1-14.6944-101.376 255.6928 255.6928 0 0 0-62.976-36.4544 137.8816 137.8816 0 0 1-95.232 37.9904 137.8816 137.8816 0 0 1-95.232-37.9904 255.6928 255.6928 0 0 0-62.976 36.4032c8.192 33.9968 3.328 70.1952-14.6944 101.4272zM512 636.2112a138.24 138.24 0 1 1 0-276.48 138.24 138.24 0 0 1 0 276.48z m0-51.2a87.04 87.04 0 1 0 0-174.08 87.04 87.04 0 0 0 0 174.08z",
                            Color.WHITE);
        set.setSize(20);

        setButton.setGraphic(set);
        setButton.setRipplerFill(Color.WHITE);
        setButton.setOnMouseClicked(
                new EventHandler<MouseEvent>() {
                    @Override
                    public void handle(MouseEvent mouseEvent) {
                        try {
                            editSetting();
                        } catch (AWTException | IOException | ParseException e) {
                            e.printStackTrace();
                        }
                    }
                }
        );
        
        rightToolBar.getChildren().add(deletePeriod);
        rightToolBar.getChildren().add(deleteCourse);
        rightToolBar.getChildren().add(addPeriod);
        rightToolBar.getChildren().add(addCourse);
        rightToolBar.getChildren().add(setButton);

        buttonBar.setRight(rightToolBar);

        root.setTop(buttonBar);
        HBox hBox = new HBox();
        hBox.setAlignment(Pos.CENTER);
        ScrollPane scrollPane = new ScrollPane();
        HBox scheduleTable = new HBox();
        scheduleTable.setAlignment(Pos.CENTER);
        scheduleTable.getChildren().add(ScheduleCell.generatePeriodSpan(GlobalConfig.Schedule));


        ArrayList<ArrayList<Period>> periods = new ArrayList<ArrayList<Period>>();
        for(int i = 0; i < 7;i ++){
            periods.add(new ArrayList<Period>());
            for(int j = 0; j < GlobalConfig.Schedule.getMorningPeriodSum() + GlobalConfig.Schedule.getAfternoonPeriodSum() + GlobalConfig.Schedule.getEveningPeriodSum();j++){
                periods.get(i).add(new Period());
            }
        }
        for(Period period : GlobalConfig.Schedule.getPeriodsList()){
            periods.get(period.getDay()).get(period.getStart()).setCourse(period.getCourse());
            periods.get(period.getDay()).get(period.getStart()).setNone(false);
        }

        for(int i = 0;i < 7;i++){
            String name = "星期";
            switch (i){
                case 0: name += "一"; break;
                case 1: name += "二"; break;
                case 2: name += "三"; break;
                case 3: name += "四"; break;
                case 4: name += "五"; break;
                case 5: name += "六"; break;
                case 6: name += "日"; break;
            }
            scheduleTable.getChildren().add(ScheduleCell.generateADaySchedule(name,periods.get(i)));
        }
        scrollPane.setContent(scheduleTable);
        hBox.getChildren().add(scrollPane);
        root.setCenter(hBox);
        Scene scene = new Scene(root, GlobalConfig.WINDOW_CURRENT_WIDTH, GlobalConfig.WINDOW_CURRENT_HEIGHT, Color.WHITE);
        scene.getStylesheets().add(Main.class.getResource("/css/schedule.css").toExternalForm());
        stage.setScene(scene);
    }
    private void onAddCourse() throws AWTException, IOException, ParseException {
        VBox content = new VBox();
        content.setSpacing(10);

        JFXTextField name = new JFXTextField();
        name.setPromptText("请输入课程的名称");
        name.setPrefSize(100,20);
        content.getChildren().add(name);

        JFXTextField teacher = new JFXTextField();
        teacher.setPromptText("请输入课程的讲师名字");
        teacher.setPrefSize(100,20);
        content.getChildren().add(teacher);

        JFXTextField classroom = new JFXTextField();
        classroom.setPromptText("请输入上课的课室");
        classroom.setPrefSize(100,20);
        content.getChildren().add(classroom);

        JFXTextField description = new JFXTextField();
        description.setPromptText("请输入备注信息");
        description.setPrefSize(100,20);
        content.getChildren().add(description);

        FlowPane main = new FlowPane();
        main.setVgap(20);
        main.setHgap(20);
        

        
        BorderPane buttonBar = new BorderPane();

        ButtonType confirm = new ButtonType("确定", ButtonBar.ButtonData.OK_DONE);
        ButtonType cancel = new ButtonType("取消", ButtonBar.ButtonData.CANCEL_CLOSE);


        DialogPane dialogPane = new DialogPane();
        dialogPane.getButtonTypes().add(cancel);
        dialogPane.getButtonTypes().add(confirm);
        dialogPane.setContent(content);
        dialogPane.setPrefWidth(200);
        dialogPane.setPrefHeight(100);


        javafx.scene.control.Dialog<ButtonType> dlg = new Dialog<ButtonType>();
        dlg.setDialogPane(dialogPane);

        Optional<ButtonType> result = dlg.showAndWait();
        if(result.isPresent()){
            if(result.get().getButtonData() == ButtonBar.ButtonData.OK_DONE){
                if (name.getText().length() < 1){
                    getController().displayTray("输入非法","课程名字不能为空","error");
                }
                if (GlobalConfig.Schedule.existCourse(name.getText())){
                    getController().displayTray("输入非法","有重复的课程名字","error");
                }
                else{
                    JSONObject form = new JSONObject();
                    form.put("userId",GlobalConfig.USER_ID);
                    form.put("name", name.getText());
                    form.put("teacher",teacher.getText());
                    form.put("classroom",classroom.getText());
                    form.put("description",description.getText());
                    form.put("color",GlobalConfig.Color[GlobalConfig.Schedule.getCourseList().size()]);
                    GlobalConfig.Schedule.getCourseList().add(Course.parseJSON(form));
                    ScheduleDao scheduleDao = new ScheduleDaoImpl();
                    int userId = -1;
                    if(GlobalConfig.IS_LOGIN){
                        userId = GlobalConfig.USER_ID;
                    }
                    Message msg = scheduleDao.setScheduleData(userId,GlobalConfig.Schedule.toJSON());
                    pageSchedule pageSchedule = new pageSchedule(getStage(),getController());
                }
            }
            if(result.get().getButtonData() == ButtonBar.ButtonData.CANCEL_CLOSE){
                System.out.println("取消");
            }

        }


    }
    private void editSetting() throws AWTException, IOException, ParseException {
        VBox content = new VBox();
        content.setSpacing(10);

        HBox courseSelectorBar = new HBox();
        Label label1 = new Label("一节课的时长（分钟）:");
        label1.setStyle("-fx-font-size: 10px;-fx-text-fill: grey;");
        JFXTextField periodTimeSpan = new JFXTextField();
        periodTimeSpan.setPromptText("请输入一节课的时长");
        periodTimeSpan.setPrefSize(100,20);
        periodTimeSpan.setText(GlobalConfig.Schedule.getPeriodTimeSpan()+"");
        content.getChildren().add(label1);
        content.getChildren().add(periodTimeSpan);

        Label label2 = new Label("课间休息时长单位（分钟）:");
        label2.setStyle("-fx-font-size: 10px;-fx-text-fill: grey;");
        JFXTextField intervalSpan = new JFXTextField();
        intervalSpan.setPromptText("课间休息时长单位");
        intervalSpan.setPrefSize(100,20);
        intervalSpan.setText(GlobalConfig.Schedule.getIntervalSpan()+"");
        content.getChildren().add(label2);
        content.getChildren().add(intervalSpan);

        Label label3 = new Label("上午的课时数:");
        label3.setStyle("-fx-font-size: 10px;-fx-text-fill: grey;");
        JFXTextField morningPeriodSum = new JFXTextField();
        morningPeriodSum.setPromptText("请输入上午的课时数");
        morningPeriodSum.setPrefSize(100,20);
        morningPeriodSum.setText(GlobalConfig.Schedule.getMorningPeriodSum() +"");
        content.getChildren().add(label3);
        content.getChildren().add(morningPeriodSum);

        Label label4 = new Label("下午的课时数:");
        label4.setStyle("-fx-font-size: 10px;-fx-text-fill: grey;");
        JFXTextField afternoonPeriodSum = new JFXTextField();
        afternoonPeriodSum.setPromptText("请输入下午的课时数");
        afternoonPeriodSum.setPrefSize(100,20);
        afternoonPeriodSum.setText(GlobalConfig.Schedule.getAfternoonPeriodSum() +"");
        content.getChildren().add(label4);
        content.getChildren().add(afternoonPeriodSum);

        Label label5 = new Label("晚上的课时数:");
        label5.setStyle("-fx-font-size: 10px;-fx-text-fill: grey;");
        JFXTextField eveningPeriodSum = new JFXTextField();
        eveningPeriodSum.setPromptText("请输入晚上的课时数");
        eveningPeriodSum.setPrefSize(100,20);
        eveningPeriodSum.setText(GlobalConfig.Schedule.getEveningPeriodSum() +"");
        content.getChildren().add(label5);
        content.getChildren().add(eveningPeriodSum);

        Label label6 = new Label("上午第一节课的时间）:");
        label6.setStyle("-fx-font-size: 10px;-fx-text-fill: grey;");
        JFXTimePicker morningFirstPeriodTime = new JFXTimePicker(GlobalConfig.Schedule.getMorningFirstPeriodTime());
        morningFirstPeriodTime.setPromptText("请选择上午第一节课时间");
        content.getChildren().add(label6);
        content.getChildren().add(morningFirstPeriodTime);

        Label label7 = new Label("下午第一节课的时间:");
        label7.setStyle("-fx-font-size: 10px;-fx-text-fill: grey;");
        JFXTimePicker afternoonFirstPeriodTime = new JFXTimePicker(GlobalConfig.Schedule.getAfternoonFirstPeriodTime());
        afternoonFirstPeriodTime.setPromptText("请选择下午第一节课时间");
        content.getChildren().add(label7);
        content.getChildren().add(afternoonFirstPeriodTime);

        Label label8 = new Label("晚上第一节课的时间:");
        label8.setStyle("-fx-font-size: 10px;-fx-text-fill: grey;");
        JFXTimePicker eveningFirstPeriodTime = new JFXTimePicker(GlobalConfig.Schedule.getEveningFirstPeriodTime());
        eveningFirstPeriodTime.setPromptText("请选择晚上第一节课时间");
        content.getChildren().add(label8);
        content.getChildren().add(eveningFirstPeriodTime);



        BorderPane buttonBar = new BorderPane();

        ButtonType confirm = new ButtonType("确定", ButtonBar.ButtonData.OK_DONE);
        ButtonType cancel = new ButtonType("取消", ButtonBar.ButtonData.CANCEL_CLOSE);


        DialogPane dialogPane = new DialogPane();
        dialogPane.getButtonTypes().add(cancel);
        dialogPane.getButtonTypes().add(confirm);
        dialogPane.setContent(content);
        dialogPane.setPrefWidth(300);
        dialogPane.setPrefHeight(100);


        javafx.scene.control.Dialog<ButtonType> dlg = new Dialog<ButtonType>();
        dlg.setDialogPane(dialogPane);

        Optional<ButtonType> result = dlg.showAndWait();
        if(result.isPresent()){
            if(result.get().getButtonData() == ButtonBar.ButtonData.OK_DONE){
                if (periodTimeSpan.getText().length() < 1 || intervalSpan.getText().length() < 1 || morningPeriodSum.getText().length() < 1 || afternoonPeriodSum.getText().length() < 1 || eveningPeriodSum.getText().length() < 1 || morningFirstPeriodTime.getValue().toString().length() < 1 || afternoonFirstPeriodTime.getValue().toString().length() < 1 || eveningFirstPeriodTime.getValue().toString().length() < 1){
                    getController().displayTray("输入非法","所有属性均均不能为空","error");
                }
                else{
                    JSONObject form = new Schedule(Integer.parseInt(periodTimeSpan.getText()) ,Integer.parseInt(intervalSpan.getText()),morningFirstPeriodTime.getValue(),afternoonFirstPeriodTime.getValue(),eveningFirstPeriodTime.getValue(),Integer.parseInt(morningPeriodSum.getText()),Integer.parseInt(afternoonPeriodSum.getText()),Integer.parseInt(eveningPeriodSum.getText())).exportSetting();
                    GlobalConfig.Schedule.setSetting(form);
                    ScheduleDao scheduleDao = new ScheduleDaoImpl();
                    int userId = -1;
                    if(GlobalConfig.IS_LOGIN){
                        userId = GlobalConfig.USER_ID;
                    }
                    Message msg = scheduleDao.setScheduleData(userId,GlobalConfig.Schedule.toJSON());
                    pageSchedule pageSchedule = new pageSchedule(getStage(),getController());
                }
            }
            if(result.get().getButtonData() == ButtonBar.ButtonData.CANCEL_CLOSE){
                System.out.println("取消");
            }

        }
    }
    private void onDeletePeriod() throws AWTException, IOException, ParseException {
        VBox content = new VBox();
        content.setSpacing(10);

        HBox deletePeriodBar = new HBox();

        deletePeriodField = new JFXTextField();
        deletePeriodField.setPromptText("选择要删除的课时");
        deletePeriodField.setPrefSize(100,20);
        deletePeriodField.setDisable(true);
        deletePeriodField.setMinWidth(200);

        deletePeriodBar.getChildren().add(deletePeriodField);
        deletePeriodBar.setAlignment(Pos.CENTER);
        deletePeriodBar.setPrefWidth(300);
//        deletePeriodBar.setPadding(new Insets(20,20,20,20));

        JFXHamburger show = new JFXHamburger();
        JFXRippler rippler = new JFXRippler(show, JFXRippler.RipplerMask.CIRCLE, JFXRippler.RipplerPos.BACK);
        JFXListView<javafx.scene.control.Label> list = new JFXListView<>();
        JFXPopup popup = new JFXPopup(list);
        int i = 0;
        for (Period period: GlobalConfig.Schedule.getPeriodsList()) {
            String name = "星期";
            switch (period.getDay()){
                case 0: name += "一"; break;
                case 1: name += "二"; break;
                case 2: name += "三"; break;
                case 3: name += "四"; break;
                case 4: name += "五"; break;
                case 5: name += "六"; break;
                case 6: name += "日"; break;
            }
            Label label =  new Label(name + "第"+ (period.getStart()+1) +"节 " +period.getCourse().getName());
            label.setId(String.valueOf(i));
            list.setOnMouseClicked(e -> {
                popup.hide();

            });
            list.getItems().add(label);
            i++;
        }

        list.getSelectionModel().selectedItemProperty().addListener(new deleteListItemChangeListener());


        AnchorPane container = new AnchorPane();
        container.getChildren().add(rippler);
        rippler.setOnMouseClicked(e -> popup.show(rippler, JFXPopup.PopupVPosition.TOP, JFXPopup.PopupHPosition.LEFT));
        deletePeriodBar.getChildren().add(container);

        content.getChildren().add(deletePeriodBar);
        FlowPane main = new FlowPane();
        main.setVgap(30);
        main.setHgap(30);
        BorderPane buttonBar = new BorderPane();
        ButtonType confirm = new ButtonType("确定", ButtonBar.ButtonData.OK_DONE);
        ButtonType cancel = new ButtonType("取消", ButtonBar.ButtonData.CANCEL_CLOSE);


        DialogPane dialogPane = new DialogPane();
        dialogPane.getButtonTypes().add(cancel);
        dialogPane.getButtonTypes().add(confirm);
        dialogPane.setContent(content);
        dialogPane.setPrefWidth(300);
        dialogPane.setPrefHeight(100);


        javafx.scene.control.Dialog<ButtonType> dlg = new Dialog<ButtonType>();
        dlg.setDialogPane(dialogPane);

        Optional<ButtonType> result = dlg.showAndWait();
        if(result.isPresent()){
            if(result.get().getButtonData() == ButtonBar.ButtonData.OK_DONE){
                    GlobalConfig.Schedule.getPeriodsList().remove(deletePeriodIndex);
                    deletePeriodIndex =  -1;
                    ScheduleDao scheduleDao = new ScheduleDaoImpl();
                    int userId = -1;
                    if(GlobalConfig.IS_LOGIN){
                        userId = GlobalConfig.USER_ID;
                    }
                    Message msg = scheduleDao.setScheduleData(userId,GlobalConfig.Schedule.toJSON());
                    pageSchedule pageSchedule = new pageSchedule(getStage(),getController());

            }
            if(result.get().getButtonData() == ButtonBar.ButtonData.CANCEL_CLOSE){
                System.out.println("取消");
            }

        }
    }
    private void onDeleteCourse() throws AWTException, IOException, ParseException {
        VBox content = new VBox();
        content.setSpacing(10);

        HBox deleteCourseBar = new HBox();

        deleteCourseField = new JFXTextField();
        deleteCourseField.setPromptText("选择要删除的课时");
        deleteCourseField.setPrefSize(100,20);
        deleteCourseField.setDisable(true);
        deleteCourseField.setMinWidth(200);

        deleteCourseBar.getChildren().add(deleteCourseField);
        deleteCourseBar.setAlignment(Pos.CENTER);
        deleteCourseBar.setPrefWidth(300);

        JFXHamburger show = new JFXHamburger();
        JFXRippler rippler = new JFXRippler(show, JFXRippler.RipplerMask.CIRCLE, JFXRippler.RipplerPos.BACK);
        JFXListView<javafx.scene.control.Label> list = new JFXListView<>();
        JFXPopup popup = new JFXPopup(list);
        int i = 0;
        for(Course course: GlobalConfig.Schedule.getCourseList()) {
            Label label =  new Label(course.getName());
            label.setId(String.valueOf(i));
            list.setOnMouseClicked(e -> {
                popup.hide();

            });
            list.getItems().add(label);
            i++;
        }

        list.getSelectionModel().selectedItemProperty().addListener(new deleteCourseListItemChangeListener());


        AnchorPane container = new AnchorPane();
        container.getChildren().add(rippler);
        rippler.setOnMouseClicked(e -> popup.show(rippler, JFXPopup.PopupVPosition.TOP, JFXPopup.PopupHPosition.LEFT));
        deleteCourseBar.getChildren().add(container);

        content.getChildren().add(deleteCourseBar);
        FlowPane main = new FlowPane();
        main.setVgap(30);
        main.setHgap(30);
        BorderPane buttonBar = new BorderPane();
        ButtonType confirm = new ButtonType("确定", ButtonBar.ButtonData.OK_DONE);
        ButtonType cancel = new ButtonType("取消", ButtonBar.ButtonData.CANCEL_CLOSE);


        DialogPane dialogPane = new DialogPane();
        dialogPane.getButtonTypes().add(cancel);
        dialogPane.getButtonTypes().add(confirm);
        dialogPane.setContent(content);
        dialogPane.setPrefWidth(300);
        dialogPane.setPrefHeight(100);


        javafx.scene.control.Dialog<ButtonType> dlg = new Dialog<ButtonType>();
        dlg.setDialogPane(dialogPane);

        Optional<ButtonType> result = dlg.showAndWait();
        if(result.isPresent()){
            if(result.get().getButtonData() == ButtonBar.ButtonData.OK_DONE){
                GlobalConfig.Schedule.getCourseList().remove(deleteCourseIndex);
                deletePeriodIndex =  -1;
                ScheduleDao scheduleDao = new ScheduleDaoImpl();
                int userId = -1;
                if(GlobalConfig.IS_LOGIN){
                    userId = GlobalConfig.USER_ID;
                }
                Message msg = scheduleDao.setScheduleData(userId,GlobalConfig.Schedule.toJSON());
                pageSchedule pageSchedule = new pageSchedule(getStage(),getController());

            }
            if(result.get().getButtonData() == ButtonBar.ButtonData.CANCEL_CLOSE){
                System.out.println("取消");
            }

        }
    }
    private void onAddPeriod() throws AWTException, IOException, ParseException {
        VBox content = new VBox();
        content.setSpacing(10);

        HBox courseSelectorBar = new HBox();

        courseField = new JFXTextField();
        courseField.setPromptText("选择课程");
        courseField.setPrefSize(100,20);
        courseField.setDisable(true);
        courseSelectorBar.getChildren().add(courseField);
        courseSelectorBar.setAlignment(Pos.CENTER);

        JFXHamburger show = new JFXHamburger();
        JFXRippler rippler = new JFXRippler(show, JFXRippler.RipplerMask.CIRCLE, JFXRippler.RipplerPos.BACK);
        JFXListView<javafx.scene.control.Label> list = new JFXListView<>();
        JFXPopup popup = new JFXPopup(list);
        for (Course course: GlobalConfig.Schedule.getCourseList()) {

            Label label =  new Label(course.getName());
            list.setOnMouseClicked(e -> {
                popup.hide();

            });
            list.getItems().add(label);
        }

        list.getSelectionModel().selectedItemProperty().addListener(new NoticeListItemChangeListener());


        AnchorPane container = new AnchorPane();
        container.getChildren().add(rippler);
        rippler.setOnMouseClicked(e -> popup.show(rippler, JFXPopup.PopupVPosition.TOP, JFXPopup.PopupHPosition.LEFT));
        courseSelectorBar.getChildren().add(container);

        content.getChildren().add(courseSelectorBar);

        Label tips = new Label("*请输入汉字数字,如一");
        tips.setStyle("-fx-font-size: 10px;-fx-text-fill: grey;");
        content.getChildren().add(tips);

        JFXTextField day = new JFXTextField();
        day.setPromptText("请输入星期几上课");
        day.setPrefSize(100,20);
        content.getChildren().add(day);



        JFXTextField index = new JFXTextField();
        index.setPromptText("第几节课上课");
        index.setPrefSize(100,20);
        content.getChildren().add(index);


        FlowPane main = new FlowPane();
        main.setVgap(20);
        main.setHgap(20);



        BorderPane buttonBar = new BorderPane();

        ButtonType confirm = new ButtonType("确定", ButtonBar.ButtonData.OK_DONE);
        ButtonType cancel = new ButtonType("取消", ButtonBar.ButtonData.CANCEL_CLOSE);


        DialogPane dialogPane = new DialogPane();
        dialogPane.getButtonTypes().add(cancel);
        dialogPane.getButtonTypes().add(confirm);
        dialogPane.setContent(content);
        dialogPane.setPrefWidth(300);
        dialogPane.setPrefHeight(100);


        javafx.scene.control.Dialog<ButtonType> dlg = new Dialog<ButtonType>();
        dlg.setDialogPane(dialogPane);

        Optional<ButtonType> result = dlg.showAndWait();
        if(result.isPresent()){
            if(result.get().getButtonData() == ButtonBar.ButtonData.OK_DONE){
                if (courseField.getText().length() < 1 || index.getText().length() < 1 || day.getText().length() < 1){
                    getController().displayTray("输入非法","课程,星期几,第几节课上均不能为空","error");
                }
                if (Integer.parseInt(index.getText()) > GlobalConfig.Schedule.getMorningPeriodSum() + GlobalConfig.Schedule.getAfternoonPeriodSum() + GlobalConfig.Schedule.getEveningPeriodSum()){
                    getController().displayTray("输入非法","超过一天的课时上限","error");
                }
                if(!day.getText().equals("一") && !day.getText().equals("二") &&!day.getText().equals("三") &&!day.getText().equals("四") &&!day.getText().equals("五") &&!day.getText().equals("六") && !day.getText().equals("日")){
                    getController().displayTray("输入非法","输入的值星期值只能为一二三四五六日","error");
                }
                else{
                    JSONObject form = new JSONObject();
                    form.put("course", GlobalConfig.Schedule.getCourseByName(courseField.getText()).toJSON().toString());
                    switch (day.getText()){
                        case "一":form.put("day",0);break;
                        case "二":form.put("day",1);break;
                        case "三":form.put("day",2);break;
                        case "四":form.put("day",3);break;
                        case "五":form.put("day",4);break;
                        case "六":form.put("day",5);break;
                        case "日":form.put("day",6);break;
                    }
                    form.put("start",(Integer.parseInt(index.getText()) - 1));
                    form.put("color",GlobalConfig.Color[GlobalConfig.Schedule.getCourseList().size()]);
                    GlobalConfig.Schedule.getPeriodsList().add(Period.parseJSON(form));
                    ScheduleDao scheduleDao = new ScheduleDaoImpl();
                    int userId = -1;
                    if(GlobalConfig.IS_LOGIN){
                        userId = GlobalConfig.USER_ID;
                    }
                    Message msg = scheduleDao.setScheduleData(userId,GlobalConfig.Schedule.toJSON());
                    pageSchedule pageSchedule = new pageSchedule(getStage(),getController());
                }
            }
            if(result.get().getButtonData() == ButtonBar.ButtonData.CANCEL_CLOSE){
                System.out.println("取消");
            }

        }
    }

    private class NoticeListItemChangeListener implements ChangeListener<Object> {
        @Override
        public void changed(ObservableValue<? extends Object> observable, Object oldValue, Object newValue) {
            selectCourse = ((Label)newValue).getText();
            courseField.setText(((Label)newValue).getText());
        }
    }
    private class deleteListItemChangeListener implements ChangeListener<Object> {
        @Override
        public void changed(ObservableValue<?> observable, Object oldValue, Object newValue) {
            System.out.println(observable);
            deletePeriodField.setText(((Label)newValue).getText());
            deletePeriodIndex = Integer.parseInt(((Label)newValue).getId());
        }
    }
    private class deleteCourseListItemChangeListener implements ChangeListener<Object> {
        @Override
        public void changed(ObservableValue<?> observable, Object oldValue, Object newValue) {
            System.out.println(observable);
            deleteCourseField.setText(((Label)newValue).getText());
            deleteCourseIndex = Integer.parseInt(((Label)newValue).getId());
        }
    }
}
